package com.niiwoo.civet.trade.service.dubbo.loan.core;

import com.alibaba.fastjson.JSON;
import com.niiwoo.asset.constant.MqConstants;
import com.niiwoo.asset.dto.mq.R360OrderStatusChangedDTO;
import com.niiwoo.asset.enums.r360.R360OrderProjectSubStatusEnum;
import com.niiwoo.asset.enums.r360.R360OrderStatusEnum;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dto.request.loan.PushProjectRequestDTO;
import com.niiwoo.civet.trade.enums.TradeConfigEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: liaozuliang
 * @date: 2018/9/20
 */
@Slf4j
@Service
public class R360SpeedLoanServiceImpl extends AbstractSpeedLoan {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    protected void sendInstationInfo(Project project) {
        log.info("融360-推标-不需要发站内信息：projectId={}", project.getProjectId());
    }

    @Override
    protected void pushProjectSuccess(PushProjectRequestDTO pushProjectRequestDTO, Project project) {
        super.pushProjectSuccess(pushProjectRequestDTO, project);
        this.sendMQForNotify(project);
    }

    @Override
    protected TradeConfigEnum getTradeConfigEnum() {
        return super.getTradeConfigEnum();
    }

    private void sendMQForNotify(Project project) {
        try {
            R360OrderStatusChangedDTO r360OrderStatusChangedDTO = new R360OrderStatusChangedDTO();
            r360OrderStatusChangedDTO.setProjectId(project.getProjectId());
            r360OrderStatusChangedDTO.setR360OrderStatusEnumCode(R360OrderStatusEnum.CONFIRM_LOAN.getCode());
            r360OrderStatusChangedDTO.setR360OrderProjectSubStatusEnum(R360OrderProjectSubStatusEnum.COLLECTING_INIT);
            r360OrderStatusChangedDTO.setUpdateDate(new Date());

            rabbitTemplate.convertAndSend(MqConstants.Exchange.ASSET_EXCHANGE, MqConstants.getRoutingKey(MqConstants.Exchange.ASSET_EXCHANGE, MqConstants.Queue.ORDER_STATUS_NOFIFY_FOR_COMMON), r360OrderStatusChangedDTO);
            log.info("融360订单确认借款(存管上标成功)，发关MQ信息通知资产端，project:{}，MQContent:{}", JSON.toJSONString(project), JSON.toJSONString(r360OrderStatusChangedDTO));
        } catch (Exception e) {
            log.error("融360-推标，MQ发送标的信息失败：project={}", JSON.toJSONString(project));
        }
    }
}
